package net.jlibrary.common.project.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.CodeGenComment;
import net.jlibrary.common.core.annotation.Compositor;
import net.jlibrary.common.core.annotation.PropertyVoPo;
import net.jlibrary.common.core.annotation.PropertyVoPoCondition;
import net.jlibrary.common.core.annotation.SearchCondition;
import net.jlibrary.common.core.annotation.SearchField;
import net.jlibrary.common.core.annotation.SeePurview;
import net.jlibrary.common.core.annotation.ValidateSign;
import net.jlibrary.common.core.entity.BaseObject;
import net.jlibrary.common.core.entity.IAccount;
import net.jlibrary.common.core.entity.IValid;
import net.jlibrary.common.tools.MD5Encrypt;

/**
 * 用户基础表
 * 
 * @author sunlf
 *
 */
@Compositor
@SeePurview(value = "group.globalPath")
@ValidateSign(value = "sign", validSign = "validSign", toException = false)
@Entity(name = "tab_account")
@Inheritance(strategy = InheritanceType.JOINED)
public class Account extends BaseObject implements IAccount, IValid {
	private static final long serialVersionUID = 8520341667033397536L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@SearchField(value = "id", condition = SearchCondition.EQUAL)
	private Long id;
	@Column(length = 50, updatable = false)
	@SearchField(value = "code", condition = SearchCondition.EQUAL)
	@CodeGenComment(value = "登录名", viewList = true, viewSearch = true, viewText = true, minLength = 2, maxLength = 20)
	private String code;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "group_id")
	@SearchField(value = "group", condition = SearchCondition.EQUAL)
	@CodeGenComment(value = "所属机构")
	private Group group;
	@ManyToMany(fetch = FetchType.EAGER)
	@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@JoinTable(name = "tab_account_role", joinColumns = @JoinColumn(name = "account_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	@CodeGenComment(value = "角色")
	@PropertyVoPo(value = PropertyVoPoCondition.Collection2Array, children = "id")
	@JsonManagedReference
	private Set<PermissionRole> roles = new HashSet<PermissionRole>();
	@Column(length = 50)
	@CodeGenComment(value = "登录密码")
	private String password;
	// ==========================================================//
	@Column(length = 50)
	@CodeGenComment(value = "昵称", viewList = true, viewSearch = true, required = true, minLength = 2, maxLength = 20)
	private String name;// 昵称
	@CodeGenComment(value = "有效性", viewList = true, viewSearch = true, required = true)
	private boolean valid;
	@Column(name = "onlines")
	@CodeGenComment(value = "是否在线", viewList = true, viewSearch = true, required = true)
	private boolean online;// 是否在线
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT)
	@JsonFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT, timezone = CoreConstants.DATE_TIMEZONE_DEFAULT)
	@CodeGenComment(value = "创建时间", viewList = true)
	private Date createdate;
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT)
	@JsonFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT, timezone = CoreConstants.DATE_TIMEZONE_DEFAULT)
	private Date lastdate;
	@Column(length = 50, name = "uuid")
	@SearchField(value = "uid", condition = SearchCondition.EQUAL)
	@CodeGenComment(value = "用户唯一标识")
	private String uid;// 用户唯一标识，多系统调用使用

	@Transient
	private boolean allowQueryInvalid;
	@Transient
	private boolean compellingDelete;
	@Transient
	private Date loginTime;
	@Transient
	private Date logoutTime;
	@Transient
	private Date refurbishTime;
	@Transient
	public String sourceClient;
	@Transient
	private boolean property;
	@Transient
	private boolean parkShop;
	@Transient
	private Set<String> propertyGroupCodes = new HashSet<String>();// 用户所在物业停车场的CODE，一个用户可能属于多个停车场

	public Long getId() {
		return id;
	}

	@Override
	@Transient
	public Serializable getPrimaryKey() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Set<PermissionRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<PermissionRole> roles) {
		this.roles = roles;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = MD5Encrypt.getInstance().encrypt(password);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Date getLastdate() {
		return lastdate;
	}

	public void setLastdate(Date lastdate) {
		this.lastdate = lastdate;
	}

	public boolean isAllowQueryInvalid() {
		return allowQueryInvalid;
	}

	public void setAllowQueryInvalid(boolean allowQueryInvalid) {
		this.allowQueryInvalid = allowQueryInvalid;
	}

	public boolean isCompellingDelete() {
		return compellingDelete;
	}

	public void setCompellingDelete(boolean compellingDelete) {
		this.compellingDelete = compellingDelete;
	}

	@Override
	public boolean equals(Object rhs) {
		if (rhs == null)
			return false;
		if (!(rhs instanceof Account))
			return false;
		Account that = (Account) rhs;
		if (this.getId() == null || that.getId() == null)
			return false;
		return (this.getId().equals(that.getId()));
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	public Date getLogoutTime() {
		return logoutTime;
	}

	public void setLogoutTime(Date logoutTime) {
		this.logoutTime = logoutTime;
	}

	public Date getRefurbishTime() {
		return refurbishTime;
	}

	public void setRefurbishTime(Date refurbishTime) {
		this.refurbishTime = refurbishTime;
	}

	public boolean isOnline() {
		return online;
	}

	public void setOnline(boolean online) {
		this.online = online;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getSourceClient() {
		return sourceClient;
	}

	public void setSourceClient(String sourceClient) {
		this.sourceClient = sourceClient;
	}

	public boolean isProperty() {
		return property;
	}

	public void setProperty(boolean property) {
		this.property = property;
	}

	public Set<String> getPropertyGroupCodes() {
		return propertyGroupCodes;
	}

	public void setPropertyGroupCodes(Set<String> propertyGroupCodes) {
		this.propertyGroupCodes = propertyGroupCodes;
	}

	public boolean isParkShop() {
		return parkShop;
	}

	public void setParkShop(boolean parkShop) {
		this.parkShop = parkShop;
	}
}
